<?php

namespace app\admin\model;

use app\admin\model\Store as modelStore;
use app\admin\validate\Category as va_Category;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\exception\ValidateException;
use think\Model;

class Store extends Model
{

    protected $autoWriteTimestamp='datetime';
    public function getStatusAttr($value)
    {
        $Status = [1=>'显示',0=>'隐藏'];
        return $Status[$value];
    }
    public function getTypeAttr($value)
    {
        $Status = [1=>'直营店',2=>'品牌店'];
        return $Status[$value];
    }

    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public  static function getList(){

        $where=[];
        $store_json=session('store_id');;
        $where[]=['id','in',$store_json];

      return  (new Store)->where($where)->page(input('page',1))->limit(input('limit',20))->select();

    }
    public static function insertCategory($add){
        try {

            $result = validate(\app\admin\validate\Store::class)->check($add);
            if(!empty($result)){

                $result=self::insert($add);
                if($result){
                    $data = array(
                        'code' => 200,
                        'msg' => '成功!',
                        'url'=>url('/nav/lists')->build()
                    );

                    return $data;
                }else{
                    $data = array(
                        'code' => 40002,
                        'msg' => '失败!',
                    );
                    return $data;
                }
                // dump($result);die;
            }
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $e->getError();
        }

    }

    public static function modifyCategory($add){
        try {


            $result = validate(\app\admin\validate\Store::class)->check(input(''));

            if (!empty($result)) {

                $result=self::where(['id'=>input('id')])->update($add);

                if(!empty($result)){
                    $data = array(
                        'code' => 200,
                        'msg' => '成功!',
                        'url'=>url('/Store/getList')->build()
                    );

                    return $data;
                }else{
                    $data = array(
                        'code' => 40002,
                        'msg' => '成功!',
                    );
                    return $data;
                }
                // dump($result);die;
            }
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息

            return $e->getError();
        }

    }


}